<script setup>
import { ref } from 'vue'
import {
  getSupplyListByQueryinfService,
  addPurchaseOrderService
} from '@/api/purchaseManage.js'
import { ElInput } from 'element-plus'
const list = ref([])
const loading = ref(false)
const queryinf = ref('')
const isQuery = ref(false)
const isSelect = ref(false)
const formRef = ref()
const selectProduct = ref({
  supplierName: '',
  contactName: '',
  contactPhone: '',
  address: '',
  productName: '',
  productDescription: '',
  categoryName: '',
  unitPrice: '',
  purchasePrice: 0,
  totalAmount: '',
  quantity: 0
})

const rules = {
  purchasePrice: [
    { required: true, message: '请输入采购单价', trigger: 'change' },
    {
      pattern: /^\d+(\.\d{1,2})?$/,
      message: '采购单价必须是两位小数以内的非负浮点数',
      trigger: 'change'
    }
  ],
  quantity: [
    { required: true, message: '请输入数量', trigger: 'change' },
    {
      pattern: /^[1-9]\d*|0$/,
      message: '数量必须是非负整数',
      trigger: 'change'
    }
  ]
}

const getlList = async () => {
  isQuery.value = true
  loading.value = true

  const res = await getSupplyListByQueryinfService(queryinf.value)
  list.value = res.data.data
  loading.value = false
}

const purchase = (row) => {
  selectProduct.value = row
  isSelect.value = true
}

const onSubmit = async () => {
  await formRef.value.validate()
  await addPurchaseOrderService(selectProduct.value)
  ElMessage.success('成功创建采购订单')
  isSelect.value = false
}
</script>

<template>
  <page-container title="创建采购订单">
    <template #extra v-if="isSelect === false">
      <ElInput v-model="queryinf" clearable style="width: 200px"></ElInput>
      <el-button @click="getlList">查询供应信息</el-button>
    </template>

    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%"
      v-if="isSelect === false"
    >
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="supplierName" label="供应商名称"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="productName" label="产品名称"></el-table-column>
      <el-table-column
        prop="productDescription"
        label="产品描述"
      ></el-table-column>
      <el-table-column prop="categoryName" label="产品分类"></el-table-column>
      <el-table-column prop="unitPrice" label="供应单价"></el-table-column>
      <el-table-column label="操作" width="200">
        <!-- row 就是 channelList 的一项， $index 下标 -->
        <template #default="{ row, $index }">
          <el-button plain type="primary" @click="purchase(row, $index)"
            >采购</el-button
          >
        </template>
      </el-table-column>

      <template #empty>
        <el-empty
          description="没有相关的供应信息"
          v-if="isQuery === true"
        ></el-empty>
        <el-empty
          description="请输入供应商名称或所需产品的关键字查询供应信息"
          v-if="isQuery === false"
        ></el-empty>
      </template>
    </el-table>

    <el-form
      ref="formRef"
      :model="selectProduct"
      :rules="rules"
      label-width="100px"
      style="padding-right: 30px"
      v-if="isSelect === true"
    >
      <el-form-item label="供应商名称">
        <el-text>{{ selectProduct.supplierName }}</el-text>
      </el-form-item>
      <el-form-item label="联系人名称">
        <el-text>{{ selectProduct.contactName }}</el-text>
      </el-form-item>
      <el-form-item label="联系人电话">
        <el-text>{{ selectProduct.contactPhone }}</el-text>
      </el-form-item>
      <el-form-item label="供应商地址">
        <el-text>{{ selectProduct.address }}</el-text>
      </el-form-item>
      <el-form-item label="产品名称">
        <el-text>{{ selectProduct.productName }}</el-text>
      </el-form-item>
      <el-form-item label="产品描述">
        <el-text>{{ selectProduct.productDescription }}</el-text>
      </el-form-item>
      <el-form-item label="产品分类">
        <el-text>{{ selectProduct.categoryName }}</el-text>
      </el-form-item>
      <el-form-item label="供应单价">
        <el-text>{{ selectProduct.unitPrice }}</el-text>
      </el-form-item>
      <el-form-item label="采购总金额">
        <el-text>{{
          selectProduct.purchasePrice * selectProduct.quantity
        }}</el-text>
      </el-form-item>
      <el-form-item label="采购单价" prop="purchasePrice">
        <el-input
          v-model="selectProduct.purchasePrice"
          placeholder="请输入采购单价"
        ></el-input>
      </el-form-item>
      <el-form-item label="采购数量" prop="quantity">
        <el-input
          v-model="selectProduct.quantity"
          placeholder="请输入采购数量"
        ></el-input>
      </el-form-item>
      <el-button type="primary" @click="onSubmit"> 提交 </el-button>
    </el-form>
  </page-container>
</template>

<style lang="scss" scoped></style>
